﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TryTM
{
    public partial class frmMain : Form
    {
        public void Calculate_IDF()
        {
            int DocumentCount = DataTable_FILES.Rows.Count;
            int Count;
            foreach (DataRow CurRow in DataTable_WORDS.Rows)
            {
                Count = (from DataRow mi in DataTable_WORD_FILE_POSITION.Rows
                         where ((int)mi[Class_Table_WORD_FILE_POSITION_Columns.ID_Word]) == (int)CurRow[Class_Table_WORDS_Columns.ID_Word]
                         select (int)mi[Class_Table_WORD_FILE_POSITION_Columns.ID_FullPathToFile]).Distinct().Count();
                CurRow[Class_Table_WORDS_Columns.Word_IDF] =
                    (double) DocumentCount /
                    (double) Count;
            }
        }

        public void Calculate_TF_IDF()
        {
            Calculate_IDF();

            var queryResult = (from mi in
                            (from mi2 in DataTable_WORD_FILE_POSITION.AsEnumerable() 
                             select new
                             {
                                 ID_Word = mi2.Field<int>(Class_Table_WORD_FILE_POSITION_Columns.ID_Word),
                                 ID_FullPathToFile = mi2.Field<int>(Class_Table_WORD_FILE_POSITION_Columns.ID_FullPathToFile)
                             }
                            ).Distinct()
                        select new 
                        {
                            ID_FullPathToFile = mi.ID_FullPathToFile ,
                            ID_Word = mi.ID_Word,
                            Word_TF_IDF = (double)
                                ((double)(from DataRow mi3 in DataTable_WORD_FILE_POSITION.Rows
                                         where (int)mi3[Class_Table_WORD_FILE_POSITION_Columns.ID_Word] == mi.ID_Word
                                         && (int)mi3[Class_Table_WORD_FILE_POSITION_Columns.ID_FullPathToFile] == mi.ID_FullPathToFile
                                         select mi3).Count()) /
                                 ((double)(from DataRow mi4 in DataTable_WORD_FILE_POSITION.Rows
                                          where (int)mi4[Class_Table_WORD_FILE_POSITION_Columns.ID_FullPathToFile] == mi.ID_FullPathToFile
                                          select mi4).Count()) *
                                  Math.Log((from DataRow mi5 in DataTable_WORDS.Rows
                                   where (int)mi5[Class_Table_WORDS_Columns.ID_Word] == mi.ID_Word
                                   select (double)mi5[Class_Table_WORDS_Columns.Word_IDF]).ToList()[0],2)
                        }).ToList();
            DataRow CurRow;
            foreach (var QR in queryResult)
            {
                CurRow = DataTable_TF_IDF.NewRow();
                CurRow[Class_Table_TF_IDF_Columns.ID_Word] = QR.ID_Word;
                CurRow[Class_Table_TF_IDF_Columns.ID_FullPathToFile] = QR.ID_FullPathToFile;
                CurRow[Class_Table_TF_IDF_Columns.Word_TF_IDF] = QR.Word_TF_IDF;
                DataTable_TF_IDF.Rows.Add(CurRow);
            }
        }

        public void Find_KeyWords()
        {
            double RankLevel = 0.4; //если по конкретному документу у нас выше этого числа, то считаем слово ключевым
            DataRow CurRow;
            var A = (from mi in DataTable_FILES.AsEnumerable()
                    select new
                    {
                        ID_FullPathToFile = (int) mi[Class_Table_FILES_Columns.ID_FullPathToFile],
                        ID_MaxValue = (double) (from mi2 in DataTable_TF_IDF.AsEnumerable()
                                                where (int)mi2[Class_Table_TF_IDF_Columns.ID_FullPathToFile] == (int)mi[Class_Table_FILES_Columns.ID_FullPathToFile]
                                           select (double) mi2[Class_Table_TF_IDF_Columns.Word_TF_IDF]).Max()
                    }).ToList();
            foreach (var CurMaxRow in A)
            {
                var KeyWords = (from DataRow mi in DataTable_TF_IDF.Rows
                                where (int)mi[Class_Table_TF_IDF_Columns.ID_FullPathToFile] == CurMaxRow.ID_FullPathToFile &&
                                (double)mi[Class_Table_TF_IDF_Columns.Word_TF_IDF] >= (CurMaxRow.ID_MaxValue * RankLevel)
                                select new
                                {
                                    ID_FullPathToFile = (int)mi[Class_Table_TF_IDF_Columns.ID_FullPathToFile],
                                    ID_Word = (int)mi[Class_Table_TF_IDF_Columns.ID_Word]
                                }
                               ).ToList();
                foreach (var QR in KeyWords)
                {
                    CurRow = DataTable_KeyWords.NewRow();
                    CurRow[Class_Table_KeyWords_Columns.ID_Word] = QR.ID_Word;
                    CurRow[Class_Table_KeyWords_Columns.ID_FullPathToFile] = QR.ID_FullPathToFile;
                    DataTable_KeyWords.Rows.Add(CurRow);
                }
            }
        }
    }
}
